const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.json());

// 模拟支付接口
app.post('/api/payment', (req, res) => {
    const { amount, paymentMethod } = req.body;

    // 模拟支付处理时间
    setTimeout(() => {
        // 80%成功率
        const success = Math.random() > 0.2;

        if (success) {
            res.json({
                success: true,
                orderId: 'DS' + Date.now(),
                amount: amount,
                paymentMethod: paymentMethod,
                message: '支付成功'
            });
        } else {
            res.status(400).json({
                success: false,
                message: '支付失败，请重试'
            });
        }
    }, 1500);
});

// 启动服务器
const PORT = 3000;
app.listen(PORT, () => {
    console.log(`服务器运行在 http://localhost:${PORT}`);
});